//
//  ContentView.swift
//  SwiftUInational
//
//  Created by myMac on 2020/4/16.
//  Copyright © 2020 myMac. All rights reserved.
//

import SwiftUI

struct ContentView: View {
    
    @State private var showAlter = false
    @State private var alerTitle = ""
    
    @State var countries = ["中国", "美国", "英国", "日本", "法国", "德国", "意大利", "俄罗斯"]
    @State var currectCountry = Int.random(in: 0...2)
    
    
    var body: some View {
        ZStack {
            LinearGradient(gradient: Gradient(colors: [.green, .blue]), startPoint: .top, endPoint: .bottom)
                .edgesIgnoringSafeArea(.all)
            VStack(spacing: 30) {
                VStack(spacing: 10) {
                    Text("下面国家的是哪个？")
                        .foregroundColor(.white)
                    Text(countries[currectCountry])
                        .foregroundColor(.white)
                        .font(.largeTitle)
                        .fontWeight(.black)
                }
                ForEach(0..<3){ num in
                    Button(action: {
                        print(self.countries[num])
                        self.alerTitle = self.currectCountry  == num ? "答对" : "答错"
                        self.showAlter = true
                    }) {
                        Image(self.countries[num])
                            .renderingMode(.original)
                    }
                    .clipShape(Capsule())
                    .overlay(Capsule().stroke(Color.black   ,lineWidth: 1))
                    .shadow(color: Color.black.opacity(0.3), radius: 2, x: 5, y: 5)
                    
                }
                Spacer()
            }
        }
        
        
        .alert(isPresented: $showAlter) {
            Alert(title: Text(self.alerTitle), message: nil, dismissButton: .default(Text("继续")){
                    self.nex()
                }
                
            )}
    }
    func nex() {
        countries.shuffle()
        currectCountry = Int.random(in: 0...2)
        showAlter = false
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
